package cn.wizzer.app.web.modules.controllers.platform.crown;

import cn.wizzer.app.crown.modules.models.CrownOrder;
import cn.wizzer.app.crown.modules.services.order.CrownOrderService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.app.web.commons.utils.StringUtil;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.math.BigDecimal;
import java.util.Map;


/**
 * 后台投资记录
 *
 * @author
 */
@IocBean
@At("/platform/crown/order")
public class CrownOrderController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private CrownOrderService orderService;

    @At("")
    @Ok("beetl:/platform/crown/order/index.html")
    @RequiresPermissions("crown.order.list")
    public void index() {

    }


    /**
     * 投资列表
     *
     * @param orderStatus   待结算、已结算、已取消等等
     * @param betType       全场波胆、半场波胆、全场总入球
     * @param matchStage    今日、明日、已结束、赛事异常
     * @param searchKeyword 搜索关键字
     * @param searchDate    搜索日期
     * @param pageNumber    页码
     * @param pageSize      页面容量
     * @return
     */
    @At
    @Ok("json")
    @RequiresPermissions("crown.order.list")
    public Object data(@Param("userCnd") String userCnd,
                       @Param("orderCnd") String orderCnd,
                       @Param("orderStatus") String orderStatus,
                       @Param("betType") String betType,
                       @Param("matchStage") String matchStage,
                       @Param("searchKeyword") String searchKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        Pagination pagination = orderService.listPage(userCnd, orderCnd, orderStatus, betType, matchStage,
                searchKeyword, searchDate, pageNumber, pageSize);
        // 统计投注总额，根据搜索条件
        Map<String, BigDecimal> sum = orderService.searchSum(userCnd, orderCnd, orderStatus, betType, matchStage,
                searchKeyword, searchDate);
        return Result.success().addData(Result.createMap("pagination", pagination, "sum", sum));
    }


    /**
     * 修改订单备注
     *
     * @param orderId 订单id
     * @param remark  备注
     * @return
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("crown.order.edit")
    @SLog(tag = "修改订单备注", msg = "${args[0]}")
    public Object edit(@Param("id") String orderId, @Param("remark") String remark) {
        CrownOrder order = orderService.fetch(orderId);
        order.setOpBy(StringUtil.getPlatformUid());
        order.setOpAt(System.currentTimeMillis() / 1000);
        orderService.update(order);
        return Result.success();
    }


    /**
     * 手动结算
     *
     * @param orderId 订单id
     * @return
     */
    @At("/manual/?")
    @Ok("json")
    @RequiresPermissions("crown.order.settle")
    @SLog(tag = "手动结算", msg = "${args[0]}")
    public Object manual(@Param("id") String orderId) {
        orderService.settleManual(orderId, StringUtil.getPlatformUid());
        return Result.success();
    }


//    @At("/artificial")
//    @Ok("json")
//    @RequiresPermissions("crown.order.settle")
//    @SLog(tag = "人工结算", msg = "${args[0]}")
//    public Object artificial(@Param("id") String orderId, @Param("amount") BigDecimal amount, @Param("remark") String remark) {
//        orderService.artificial(orderId, amount, remark, StringUtil.getPlatformUid());
//        return Result.success();
//    }
//
//    @At("/revert")
//    @Ok("json")
//    @RequiresPermissions("crown.order.revert")
//    @SLog(tag = "撤销订单", msg = "${args[0]}")
//    public Object revert(@Param("id") String orderId) {
//        orderService.revert(orderId, StringUtil.getPlatformUid());
//        return Result.success();
//    }

}
